<template>
  <el-dialog :title="title" :visible="showDialog" @close="cancelDialog">
    <el-form :model="form" :rules="rules" ref="addForm">
      <el-form-item label="部门名称" prop="name" label-width="100px">
        <el-input
          v-model="form.name"
          autocomplete="off"
          placeholder="1-50个字符"
        ></el-input>
      </el-form-item>
      <el-form-item label="部门编码" prop="code" label-width="100px">
        <el-input
          v-model="form.code"
          autocomplete="off"
          placeholder="1-50个字符"
        ></el-input>
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager" label-width="100px">
        <el-select
          v-model="form.manager"
          placeholder="请选择负责人"
          style="width:100%"
          @focus="getSimpleList"
        >
          <el-option
            v-for="item in empList"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce" label-width="100px">
        <el-input
          type="textarea"
          :rows="5"
          v-model="form.introduce"
          autocomplete="off"
          placeholder="1-300个字符"
        ></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancelDialog">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {
  getDepartments,
  addDepartments,
  getDeptsDetail,
  editDepts
} from '@/api/departments'
import { getEmployees } from '@/api/employees'
export default {
  props: {
    pid: {
      required: true,
      type: String
    },
    showDialog: {
      // 让外界传进来是显示还是隐藏
      type: Boolean,
      default: false
    }
  },
  data () {
    //   测试函数
    const checkName = async (rule, value, callback) => {
      let res = await getDepartments()
      if (this.form.id) {
        let list = res.data.depts.filter(item => item.pid == this.form.pid)
        let repeat = list.some(
          item => item.name == value && item.name != this.oldForm.name
        )
        repeat
          ? callback(new Error('不能存在相同部门的名称,请重新输入!'))
          : callback()
      } else {
        let list = res.data.depts.filter(item => {
          if (item.pid == this.pid) {
            return true
          }
        })

        //   遍历找到的所有子节点,判断是否一样
        for (let i = 0; i < list.length; i++) {
          if (list[i].name == value) {
            return callback(new Error('你输入的名称已存在,请重新输入!'))
          }
        }
        callback()
      }
    }
    const checkCode = async (rule, value, callback) => {
      let res = await getDepartments()
      let repeat
      if (this.form.id) {
        //   编辑
        repeat = res.data.depts.some(
          item => item.code === value && item.code != this.oldForm.code
        )
      } else {
        repeat = res.data.depts.some(item => item.code === value)
      }
      repeat ? callback(new Error('部门编码已经存在,不能重复!')) : callback()
    }
    return {
      // 保存员工列表的数组
      empList: [],
      //   表单绑定的数据
      form: {},
      //   用来保存原本的名字
      oldForm: {
        name: '',
        code: ''
      },
      //   表单绑定的规则
      rules: {
        name: [
          {
            required: true,
            message: '请输入..',
            trigger: 'blur'
          },
          { min: 1, max: 50, message: '内容在1-50长度', trigger: 'blur' },
          { validator: checkName, trigger: 'blur' }
        ],
        code: [
          {
            required: true,
            message: '请输入..',
            trigger: 'blur'
          },
          { min: 1, max: 50, message: '内容在1-50长度', trigger: 'blur' },
          { validator: checkCode, trigger: 'blur' }
        ],
        manager: [
          {
            required: true,
            message: '请输入..',
            trigger: 'blur'
          }
        ],
        introduce: [
          {
            required: true,
            message: '请输入..',
            trigger: 'blur'
          },
          { min: 1, max: 50, message: '内容在1-300长度', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    cancelDialog () {
      // 清空表单
      this.form = {}
      // 清空校准验证
      this.$refs.addForm.resetFields()
      // 点击隐藏编辑框
      this.$emit('update:showDialog', false);
    },
    async getSimpleList () {
      let res = await getEmployees()
      console.log(res)
      this.empList = res.data
    },
    save () {
      this.$refs.addForm.validate(async v => {
        if (v) {
          if (this.form.id) {
            await editDepts(this.form)
            this.$message.success('修改部门成功')
          } else {
            await addDepartments({
              ...this.form,
              pid: this.pid
            })
            this.$message.success('新增部门成功')
          }
          this.$emit('addDept')
          this.$emit('update:showDialog', false)
        }
      })
    },
    async getDetail (id) {
      let res = await getDeptsDetail(id)
      this.form = res.data
      this.oldForm.name = this.form.name
      this.oldForm.code = this.form.code
    }
  },

  computed: {
    title () {
      return this.form.id ? '编辑部门' : '新增部门'
    }
  }
}
</script>

<style></style>
